package I.O;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

//文件夹拷贝
public class CopyDir {
	// 1.递归查找子孙级文件/文件夹
	// 2.文件复制，文件夹创建
	public static void main(String[] args) {
		String f = "D:\\test";
		String d = "D:\\ztest";
		File f1 = new File(f);
		File f2 = new File(d);
		try {
			copyDir(f1,f2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void copyDir(File f1, File f2) throws FileNotFoundException, IOException {
		if (f1.isDirectory()) {
			f2 = new File(f2, f1.getName());
			System.out.println(f1.getName());
			copyDirDetail(f1, f2);
		}
	}

	public static void copyDirDetail(File f1, File f2) throws FileNotFoundException, IOException {

		if (f1.isFile()) {
			FileUtils.fileCopy(f1, f2);
		} else if (f1.isDirectory()) {
			f2.mkdirs();
			for (File sub : f1.listFiles()) {
				System.out.println(sub.getName());
				copyDirDetail(sub, new File(f2, sub.getName()));
			}
		}
	}
}
